/*-----------------------------------------------------------------------------------
Name: Hyuk Son
Date: Jul 22 2021
This do file does : 
	define user-written programs
-----------------------------------------------------------------------------------*/

****************************************************
/* 		Program to generate indices		 */
****************************************************
capture program drop genindex
program define genindex
	syntax varlist, prefix(string) label(string)
	foreach var of varlist `varlist' {
		qui sum `var' if control==1
		local mean_`var'=`r(mean)'
		local sd_`var'=`r(sd)'
		gen `var'_nrm=(`var' - `mean_`var'')/`sd_`var''
		local vars_norm `vars_norm' `var'_nrm
		} 
	egen `prefix'_index=rowmean(`vars_norm')
	qui sum `prefix'_index if control==1
	replace `prefix'_index=(`prefix'_index-r(mean))/r(sd)
	label var `prefix'_index "`label'"
	drop `vars_norm'
	end
	
****************************************************
/* 				 Table Format			 */
****************************************************
* Old version (before Apr 2017)
	capture program drop head_foot
	program define head_foot

	syntax, [caption(str)] [label(str)] [notes(str)] [size(str)] [columns(str)] [sideways]

	local estimates=r(names)
	if "`columns'"=="" local columns:word count `estimates'
	if "`sideways'"=="sideways" local table sidewaystable
	else local table table

	global header \begin{`table'}[htbp] 
		if "`size'"!="" global header $header \begin{`size'}
		global header $header \centering ///
		\begin{threeparttable} ///
		\def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi} 
		if "`caption'"!="" global header $header \caption{`caption'\label{tab:`label'}} 
		global header $header ///
		\begin{tabular}{l*{`columns'}{c}}\hline
		
	global footer \hline ///
		\end{tabular} 
		if "`notes'"!="" {
			global footer $footer \begin{tablenotes}[flushleft] ///
			`notes' ///
			\end{tablenotes} 
			}
		global footer $footer \end{threeparttable} 
		if "`size'"!="" global footer $footer \end{`size'}
		global footer $footer \end{`table'} 
		
	end

 * New version (after April 2017) 
	capture program drop autoformat
	program define autoformat

	if int(`1')==`1' global format "%12.0f"
	else if abs(`1')>10 global format "%9.1f"
	else if abs(`1')>1 global format "%9.2f"
	else if abs(`1')>.1 global format "%9.3f"
	else if abs(`1')>.01 global format "%9.3f"
	else if abs(`1')>.001 global format "%9.3f"
	else if abs(`1')>.0001 global format "%9.4f"
	else if abs(`1')<.00001 global format "%9.5f"

	end

 ** Tex format
  	capture program drop head_mid
	program define head_mid

	syntax, [caption(str)] [label(str)] [notes(str)] [size(str)] [columns(str)] [sideways]

	local estimates=r(names)
	if "`columns'"=="" local columns:word count `estimates'
	if "`sideways'"=="sideways" local table sidewaystable
	else local table table
	local col = `columns'+1

	global header1 \begin{`table'}[htbp] \begin{center}
		if "`size'"!="" global header1 $header1 \begin{`size'}
		global header1 $header1  ///
		\begin{threeparttable} ///
		\def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi} 
		if "`caption'"!="" global header1 $header1 \caption{`caption'\label{tab:`label'}} 
		global header1 $header1 ///
		\begin{tabular}{l*{`columns'}{c}}\hline

	global mid1 \multicolumn{`col'}{l}{Panel A: Average treatment effects}\tabularnewline \hline
	global mid1_2 \multicolumn{`col'}{l}{Panel A}\tabularnewline \hline

	global footer1 \hline 

	end


	capture program drop mid_foot
	program define mid_foot

	syntax, [caption(str)] [label(str)] [notes(str)] [size(str)] [columns(str)] [sideways]

	local estimates=r(names)
	if "`columns'"=="" local columns:word count `estimates'
	if "`sideways'"=="sideways" local table sidewaystable
	else local table table
	local col = `columns'+1
	

	global header2 \tabularnewline

	global mid2 \multicolumn{`col'}{l}{Panel B: Heterogeneous treatment effects by overall rank}\tabularnewline\hline
	global mid3 \multicolumn{`col'}{l}{Panel C: Hegerogeneous treatment effects by bin rank}\tabularnewline\hline
	global mid2_2 \multicolumn{`col'}{l}{Panel B}\tabularnewline \hline
	global mid3_2 \multicolumn{`col'}{l}{Panel C}\tabularnewline \hline

	global footer2 \hline ///
		\end{tabular} 
		if "`notes'"!="" {
			global footer2 $footer2 \begin{tablenotes}[flushleft] ///
			`notes' ///
			\end{tablenotes} 
			}
		global footer2 $footer2 \end{threeparttable} 
		if "`size'"!="" global footer2 $footer2 \end{`size'}
		global footer2 $footer2 \end{center} \end{`table'} 
		
	end

****************************************************
/* 				 Plot			 */
****************************************************
*** Plotting the graph
cap prog drop plotgraph // Plot the estimated coefficients
program plotgraph
	syntax varlist, [cat1(string)] [cat2(string)] [cat3(string)] [cl1(string)] [cl2(string)] [cl3(string)] [lp1(string)] [lp2(string)] [lp3(string)] [label1(string)] [label2(string)] [label3(string)] xtitle(string) title(string) [suffix(string)] [ylabel(string)]
	
	 ** set up
		tokenize `varlist'
		local y `1'
		macro shift
		local x `*'

	*** Plot graphs
	graph twoway lpoly `y' `x' if `cat1'==1, lc(`cl1') lp(`lp1')  || lpoly `y' `x' if `cat2'==1, lc(`cl2') lp(`lp2')  || lpoly `y' `x'  if `cat3'==1, lc(`cl3') lp(`lp3') ///
	xline(85, lc(black)) legend(label(1 "`label1'") label(2 "`label2'") label(3 "`label3'") rows(1)) ylabel(`ylabel') ytitle("") xtitle("`xtitle'") title("`title'") plotregion(style(none)) graphregion(color(white)) 

	graph save "$figure_path/`y'`suffix'.gph", replace
end

cap prog drop plotgraph_feed // Plot the estimated coefficients
program plotgraph_feed
	syntax varlist, [cat1(string)] [cat2(string)] [cl1(string)] [cl2(string)] [lp1(string)] [lp2(string)] [label1(string)] [label2(string)] xtitle(string) title(string) [suffix(string)]
	
	 ** set up
		tokenize `varlist'
		local y `1'
		macro shift
		local x `*'

	*** Plot graphs
	graph twoway lpoly `y' `x' if `cat1'==1, lc(`cl1') lp(`lp1')  || lpoly `y' `x' if `cat2'==1, lc(`cl2') lp(`lp2')  ///
	xline(85, lc(black)) legend(label(1 "`label1'") label(2 "`label2'") rows(1)) ytitle("") xtitle("`xtitle'") title("`title'") plotregion(style(none)) graphregion(color(white)) 

	graph save "$figure_path/`y'`suffix'.gph", replace
end

*** Exporting the graph
cap prog drop exportalone 
program exportalone
syntax varlist, name(string) [path(string)]
	*** Graph export
		graph use "$figure_path/`varlist'.gph"
		graph export "$figure_path/`path'`name'.png", replace

	*** erase graph
		erase "$figure_path/`varlist'.gph" 
end

*** Exporting the graph
cap prog drop exporttwo 
program exporttwo
syntax varlist, name(string) [path(string)] [suffix1(string)] [suffix2(string)]

	 ** set up
		tokenize `varlist'
		local first `1'
		macro shift
		local second `1'

		local count: word count `varlist' 

	*** Graph export
		graph combine "$figure_path/`first'`suffix1'.gph" "$figure_path/`second'`suffix2'.gph", c(`count') ycommon plotregion(style(none)) graphregion(color(white)) iscale(1) 
		graph display, xsize(9)
		graph export "$figure_path/`path'`name'.png", replace

	*** erase graphs
		erase "$figure_path/`first'`suffix1'.gph" 
		erase "$figure_path/`second'`suffix2'.gph" 
end

*** Exporting the graph
cap prog drop exportthree
program exportthree
syntax varlist, name(string) [path(string)]  [suffix1(string)]  [suffix2(string)]  [suffix3(string)]

	 ** set up
		tokenize `varlist'
		local first `1'
		macro shift
		local second `1'
		macro shift
		local third `1'

		local count: word count `varlist' 

	*** Graph export
		graph combine "$figure_path/`first'`suffix1'.gph" "$figure_path/`first'`suffix2'.gph" "$figure_path/`first'`suffix3'.gph", c(`count') ycommon plotregion(style(none)) graphregion(color(white)) iscale(1) 
		graph display, xsize(11)
		graph export "$figure_path/`path'`name'.png", replace

	*** erase graphs
		erase "$figure_path/`first'`suffix1'.gph" 
		erase "$figure_path/`first'`suffix2'.gph" 
		erase "$figure_path/`first'`suffix3'.gph" 
end
